from bs4 import BeautifulSoup
from utils.req import get_request
import requests


headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "cache-control": "no-cache",
    "dnt": "1",
    "pragma": "no-cache",
    "priority": "u=0, i",
    "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Microsoft Edge";v="138"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"macOS"',
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
}
cookies = {
    "_Verification_Cookie": "true",
    "Hm_lvt_35811d88f61196befe2e1f070a5036df": "1753421711",
    "HMACCOUNT": "15B5479AE262C420",
    "_prevCountry": "United%20States",
    "_Country": "CN",
    "_Country_Name": "China",
    "_Currency": "CNY",
    "Drupal.visitor.commerce_currency": "CNY",
    "_topbarcache": "true",
    "ak_bmsc": "E8247AA788DE681B04F1AB29D2C60356~000000000000000000000000000000~YAAQlG2bG2IgMhqYAQAAfAkUQBy5oReY6gUN0iebH6a1ey7Zo+ylhChGgNADUYugLiEIYvxhbor8rf1Sz9uA99Ti6B+XFiqPG+S+fjbvEec4VToxrh7y1l5o+nYriLnIRgFxPWr3GuAdwSzcToGbIguM1mSHuSL6E/yxjG6o0rjLnr/JkiAxDweCfe9dUAgrZCFsrsDjzxaTzluGI5kPS6A8Q8OEvUHA4qRjuD1cQ2h+QKCsvnMqY/SI2JxwiWbuE3qm9Gt90bbYQ5AyA/Ar7mmLFf0Oxcvqvhf5gCi2ZuLUHPDXia9dFiL+B/Q/MyR0sze2/sFCGDJKKEytW93eNs86PkjTtxEqE9BiF6HeHrcmuks/mAM4pMlg87Q2BJj+pNRHUWUMSFlCl2t6JOTlBvrg/Gol7hYoMZxOEHsqQqdneNPP0zV9W/UHmiccbRZQT0G9VLO1T6oD5EENlJc=",
    "_gcl_au": "1.1.308850116.1753421713",
    "_gid": "GA1.2.904942148.1753421713",
    "_mkto_trk": "id:584-XSF-168&token:_mch-novusbio.com-ae4d1895eb2290ca9fff7bfe598469cf",
    "_username": "%7B%22uid%22%3A0%2C%22username%22%3A%22%22%2C%22et_ctry%22%3A%22CN%22%2C%22et_prevctry%22%3A%22United%20States%22%2C%22redirect_url%22%3A%22%22%2C%22novus_dist%22%3A%7B%22name%22%3A%22Bio-Techne%20China%20Co.%20Ltd.%20Bio-Techne%5Cu4e2d%5Cu56fd%5Cu5206%5Cu516c%5Cu53f8%22%2C%22address%22%3A%22%5Cu4e0a%5Cu6d77%5Cu5e02%5Cu957f%5Cu5b81%5Cu533a%5Cu957f%5Cu5b81%5Cu8def1193%5Cu53f7%5Cu957f%5Cu5b81%5Cu6765%5Cu798f%5Cu58eb%5Cu5e7f%5Cu573a%5Cu529e%5Cu516c%5Cu697cT3%5Cu7b2c20%5Cu5c42%22%2C%22city%22%3A%22%22%2C%22state%22%3A%22%22%2C%22zip%22%3A%22%22%2C%22country%22%3A%22China%22%2C%22phone%22%3A%22400%20821%203475%22%2C%22sphone%22%3A%22%20800-988-1270%20400-821-3475%20%22%2C%22fax%22%3A%22%22%2C%22nomen%22%3A%22%5Cu4e2d%5Cu56fd%22%2C%22email%22%3A%22%22%2C%22semail%22%3A%22%20%20info.cn%40bio-techne.com%20%22%2C%22icp_china%22%3A%22%5Cu6caaICP%5Cu590707019069%5Cu53f7-3%22%2C%22icp_china_link%22%3A%22https%3A%5C%2F%5C%2Fbeian.miit.gov.cn%5C%2F%22%7D%7D",
    "bm_sv": "6596726FCE2086E195BDE10B94F537D4~YAAQlG2bG9s+MhqYAQAAmAkVQByj5YxIBNcUmeFa66lirPeRTiQIwlQinWmtvlRUoFry71nenbXkd+0XRWXilnG7pDCknazpZ5asqOPosO4c5M9z3mN1blCNeKMAErTTUPl2nsIAnqMfewOU1jNva6haWYGKiV8aVEhY79IZyCRbGcdeZlopnNE2RZpjLPV9rtxp4fhzYU432c5rsO2otGNidKDbCLXjcVHdguRuIOZlFIExYdI4WFHQWRC5HznMKtfZ~1",
    "Hm_lpvt_35811d88f61196befe2e1f070a5036df": "1753421777",
    "_ga": "GA1.2.1382912654.1753421713",
    "_uetsid": "24596100691911f0ad14ad6ccfc5bcee",
    "_uetvid": "24599520691911f0806a0375540309cc",
    "_ga_396F48KBM7": "GS2.1.s1753421713$o1$g1$t1753422127$j41$l0$h0",
}
url = "https://www.novusbio.com/search"
params = {"keywords": "NB100-402SS"}
response = requests.get(url, headers=headers, params=params)


def search_product(keywords: str):
    url = "https://www.novusbio.com/search"
    params = {"keywords": keywords}
    response = get_request(url=url, headers=headers, params=params)
    soup = BeautifulSoup(response.text, "html.parser")
    return soup


def extract_product_info(soup: BeautifulSoup):
    product_info = soup.find("div", attrs={"id": "content_datasheet"})
    if product_info:
        children = product_info.findChildren(recursive=False)
        des_dom = BeautifulSoup("<div></div>", "html.parser")
        for child in children[:5]:
            des_dom.find("div").append(child)
        return str(des_dom)
    return ""


def extract_image(soup: BeautifulSoup):
    image_dom = soup.find("div", attrs={"class": "cycle-slideshow slideshows"})
    src_list = image_dom.find_all("img")
    if src_list:
        return [{"photo": "https:" + src.get("src")} for src in src_list[:5]]
    else:
        return [
            {
                "photo": "https://images.novusbio.com/design/novus_color_RGB_small.png"
                for _ in range(5)
            }
        ]


def process_data(keywords: str):
    soup = search_product(keywords)
    if soup:
        des_dom = extract_product_info(soup)
        img_list = extract_image(soup)
        return des_dom, img_list
    return None, None


if __name__ == "__main__":
    soup = search_product("NB100-402SS")
    # print(soup)
    # res = extract_product_info(soup)
    # print(res)
    res = extract_image(soup)
    print(res)
